package com.taobao.machinesystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.taobao.machinesystem.common.Result;
import com.taobao.machinesystem.common.SystemConst;
import com.taobao.machinesystem.entity.McPlan0;
import com.taobao.machinesystem.entity.McPlan1;
import com.taobao.machinesystem.entity.vo.PatrolYear;
import com.taobao.machinesystem.service.McPlan0Service;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 巡查计划表(主表) 前端控制器
 * </p>
 *
 * @author LiuJunCheng
 * @since 2022-04-22
 */
@Slf4j
@RestController
@RequestMapping("/machine_system/mc-plan0")
public class McPlan0Controller {

    @Autowired
    private McPlan0Service mcPlan0Service;

    /**
     * 创建年度机房巡查计划
     *
     * @param year
     * @param mcPlan1s
     * @return Result
     */
    @PostMapping("/create/{year}")
    public Result createMcPlan(@PathVariable("year") String year,@RequestBody List<McPlan1> mcPlan1s) {
        if (Objects.isNull(year) || Objects.isNull(mcPlan1s)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }

        boolean flag = mcPlan0Service.createMcPlan(year, mcPlan1s);
        if (flag) {
            log.info("create plan success {}", year);
            return Result.ok().setMessage(SystemConst.CREATE_SUCCESS);
        } else {
            log.error("create plan fail {}", year);
            return Result.error().setMessage(SystemConst.CREATE_FAIL);
        }
    }

    /**
     * 分页查询
     *
     * @param currentPage
     * @param size
     * @param year
     * @return
     */
    @GetMapping("/list/{currentPage}/{size}")
    public Result listMcPlan0(
            @PathVariable("currentPage") Integer currentPage,
            @PathVariable("size") Integer size,
            String year) {

        Page<McPlan0> mcPlan0Page = mcPlan0Service.listMcPlan0(currentPage, size, year);

        return Result.ok()
                .setMessage(SystemConst.SELECT_SUCCESS)
                .setMapValue("total", mcPlan0Page.getTotal())
                .setMapValue("items", mcPlan0Page.getRecords());
    }

    /**
     * 跟新操作
     *
     * @param mcPlan0
     * @return
     */
    @PutMapping("/update")
    public Result updateMcPlan(@RequestBody McPlan0 mcPlan0) {
        if (Objects.isNull(mcPlan0)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }

        mcPlan0.setYear(mcPlan0.getYear().substring(0,4));
        boolean flag = mcPlan0Service.updateMcPlan(mcPlan0);
        if (flag) {
            log.info("update plan0 success {}", mcPlan0.toString());
            return Result.ok().setMessage(SystemConst.UPDATE_SUCCESS);
        } else {
            log.error("update plan0 fail {}", mcPlan0.toString());
            return Result.error().setMessage(SystemConst.UPDATE_FAIL);
        }
    }

    /**
     * 删除计划
     *
     * @param id
     * @return Result
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteMcPlan0(@PathVariable String id) {
        boolean flag = mcPlan0Service.deleteMcPlan0ById(id);
        if (flag) {
            log.info("delete plan success {}", id);
            return Result.ok().setMessage(SystemConst.DELETE_SUCCESS);
        } else {
            log.error("delete plan fail {}", id);
            return Result.error().setMessage(SystemConst.DELETE_FAIL);
        }
    }

    /**
     * 审核方法
     *
     * @param planId
     * @return Result
     */
    @PostMapping("/checkPlan/{planId}")
    public Result checkPlan(@PathVariable String planId) {
        if (StringUtils.isEmpty(planId)) {
            return Result.error().setMessage(SystemConst.NOT_NULL_PARAMETER);
        }
        boolean flag = mcPlan0Service.checkPlan(planId);
        if (flag) {
            log.info("update checkPlan success {}", planId);
            return Result.ok().setMessage(SystemConst.CHECKPLAN_SUCCESS);
        } else {
            log.error("update checkPlan fail {}", planId);
            return Result.error().setMessage(SystemConst.CHECKPLAN_FAIL);
        }
    }

    /**
     * 获取计划
     *
     * @param id
     * @return Result
     */
    @GetMapping("/get/{id}")
    public Result getMcPlan0(@PathVariable String id) {
        McPlan0 mcPlan0 = mcPlan0Service.getById(id);
        log.info("update plan success {}", id);
        return Result.ok().setMapValue("item", mcPlan0).setMessage(SystemConst.SELECT_SUCCESS);
    }

    /**
     * 巡查对比
     * @param year
     * @return
     */
    @GetMapping("/statis-ticsdaily/patrol/{year}")
    public Result staticPatrol(@PathVariable("year") String year) {
        List<PatrolYear> patrolYears = mcPlan0Service.patrolByYear(year);
        return Result.ok().setMessage(SystemConst.SELECT_SUCCESS).setMapValue("items", patrolYears);
    }
}

